package com.dl698;

import com.dl698.bean.FrameData;
import com.dl698.buss.DL698;
import com.dl698.buss.Option;
import com.dl698.buss.LinkOption;
import com.dl698.buss.RequestOption;

/**
 * DL698 Java库主入口类
 * 提供对外的公共API接口
 * 
 * @author wind
 * @version 1.1.1
 */
public class DL698Library {

    private final DL698 dl698Instance;

    /**
     * 构造函数
     */
    private DL698Library() {
        this.dl698Instance = DL698.getInstance();
    }

    /**
     * 获取DL698实例
     * 
     * @return DL698Library实例
     */
    public static DL698Library getInstance() {
        return new DL698Library();
    }

    /**
     * 构建帧数据
     * 将数据结构转换为符合DL698.45协议的二进制帧
     * 
     * @param option 配置选项
     * @return 构建结果
     */
    public FrameData buildFrame(Option option) {
        try {
            return dl698Instance.buildFrame(option);
        } catch (Exception e) {
            FrameData errorResult = new FrameData();
            errorResult.setCode(-1);
            errorResult.setData("Build frame error: " + e.getMessage());
            return errorResult;
        }
    }

    /**
     * 解析帧数据
     * 解析DL698.45协议帧为可操作的数据结构
     * 
     * @param frameData 帧数据字节数组
     * @return 解析结果
     */
    public FrameData parseFrame(byte[] frameData) {
        try {
            return dl698Instance.parseFrame(frameData);
        } catch (Exception e) {
            FrameData errorResult = new FrameData();
            errorResult.setCode(-1);
            errorResult.setData("Parse frame error: " + e.getMessage());
            return errorResult;
        }
    }

    /**
     * 构建GET请求帧
     * 
     * @param serverAddress 服务器地址
     * @param clientAddress 客户端地址
     * @param requestData   请求数据
     * @return 构建结果
     */
    public FrameData buildGetRequest(String serverAddress, String clientAddress, Object requestData) {
        try {
            // 创建链路选项
            LinkOption linkOption = new LinkOption();
            linkOption.setServerAddress(serverAddress);
            linkOption.setClientAddress(clientAddress);

            // 创建请求选项
            RequestOption requestOption = new RequestOption();
            requestOption.setRequestMode(com.dl698.buss.Choice.GET_REQUEST);
            requestOption.setRequest(requestData);

            // 创建总选项
            Option option = new Option();
            option.setLinkOption(linkOption);
            option.setRequestOption(requestOption);

            // 调用核心业务逻辑
            return dl698Instance.buildFrame(option);
        } catch (Exception e) {
            FrameData errorResult = new FrameData();
            errorResult.setCode(-1);
            errorResult.setData("Build GET request error: " + e.getMessage());
            return errorResult;
        }
    }

    /**
     * 构建SET请求帧
     * 
     * @param serverAddress 服务器地址
     * @param clientAddress 客户端地址
     * @param requestData   请求数据
     * @return 构建结果
     */
    public FrameData buildSetRequest(String serverAddress, String clientAddress, Object requestData) {
        try {
            // 创建链路选项
            LinkOption linkOption = new LinkOption();
            linkOption.setServerAddress(serverAddress);
            linkOption.setClientAddress(clientAddress);

            // 创建请求选项
            RequestOption requestOption = new RequestOption();
            requestOption.setRequestMode(com.dl698.buss.Choice.SET_REQUEST);
            requestOption.setRequest(requestData);

            // 创建总选项
            Option option = new Option();
            option.setLinkOption(linkOption);
            option.setRequestOption(requestOption);

            // 调用核心业务逻辑
            return dl698Instance.buildFrame(option);
        } catch (Exception e) {
            FrameData errorResult = new FrameData();
            errorResult.setCode(-1);
            errorResult.setData("Build SET request error: " + e.getMessage());
            return errorResult;
        }
    }
}